/**
 * Copyright 2009 Marc Stogaitis and Mimi Sun
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.gmote.server;

import java.util.logging.Logger;

import org.gmote.common.Protocol.MouseEvent;

import android.os.RemoteException;
import android.os.SystemClock;
import android.view.IPointerController;
import android.view.IWindowManager;
import android.view.MotionEvent;

public class MouseUtil {

  final private static Logger LOGGER = Logger.getLogger(MouseUtil.class.getName());
  public static boolean buttonDown = false;

  public static void doMouseEvent(MouseEvent mouseEvent, IWindowManager wm, IPointerController pointer) {
	float[] x = new float[1];
	float[] y = new float[1];
	long curTime = SystemClock.uptimeMillis();
	
	try {
		pointer.getPosition(x, y);
	
    if (mouseEvent == MouseEvent.SINGLE_CLICK) {
      LOGGER.info("SINGLE_CLICK");
      MotionEvent down = MotionEvent.obtain(curTime, curTime, MotionEvent.ACTION_DOWN, x[0], y[0], 0);
      wm.injectPointerEvent(down, false);
      MotionEvent up = MotionEvent.obtain(curTime, curTime, MotionEvent.ACTION_UP, x[0], y[0], 0);
      wm.injectPointerEvent(up, false);
    } else if (mouseEvent == MouseEvent.RIGHT_CLICK) {
      LOGGER.info("RIGHT_CLICK");
    } else if (mouseEvent == MouseEvent.DOUBLE_CLICK) {
      LOGGER.info("DOUBLE_CLICK");
    } else if (mouseEvent == MouseEvent.LEFT_MOUSE_DOWN) {
      LOGGER.info("LEFT_MOUSE_DOWN");
      buttonDown = true;
      MotionEvent down = MotionEvent.obtain(curTime, curTime, MotionEvent.ACTION_DOWN, x[0], y[0], 0);
      wm.injectPointerEvent(down, false);
    } else if (mouseEvent == MouseEvent.LEFT_MOUSE_UP) {
      LOGGER.info("LEFT_MOUSE_UP");
      buttonDown = false;
      MotionEvent up = MotionEvent.obtain(curTime, curTime, MotionEvent.ACTION_UP, x[0], y[0], 0);
      wm.injectPointerEvent(up, false);
    }

	} catch (RemoteException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
  }
}
